define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

	//设置弹窗宽高
    Fast.config.openArea = ['50%', '70%'];
	
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'bankuai/index' + location.search,
                    add_url: 'bankuai/add',
                    edit_url: 'bankuai/edit',
                    del_url: 'bankuai/del',
                    multi_url: 'bankuai/multi',
                    import_url: 'bankuai/import',
                    table: 'bankuai',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID')},
                        {field: 'bk_name', title: __('Bk_name'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons: [
                               {
									name: 'zhiding',
									title: '指定设备',
									text: '指定设备',
									url: 'bankuai/shebei',
									classname: 'btn btn-xs btn-primary btn-dialog'
							   }
                            ],formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
		shebei: function(){
			Table.api.init({});
			
			var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: location.href,
                pk: 'id',
                sortName: 'id',
				commonSearch: false,
				showToggle: false,
				showColumns: false,
				showExport: false,
				maintainSelected: true,
                columns: [
                    [
                        {field: 'shebei', checkbox: true, formatter: function (value, row, index) {
                                return {
                                    checked: row.checked,
                                }
						}
						},
                        {field: 'device_serial', title: __('设备序列号'), operate: 'LIKE'},
                        {field: 'device_name', title: __('设备名称'), operate: 'LIKE'},
                        {field: 'device_type', title: __('设备类型')},
						{field: 'device_version', title: __('设备版本号')},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
			
			$(document).on("click", "a.btn-select", function () {
                var ids = Table.api.selectedids(table);
				Fast.api.ajax({
					url: "bankuai/bind_shebei/bk_id/"+Config.ids,
					type: "post",
					data: {ids: ids.join(",")},
				}, function (data, ret) {
					var msg = ret.hasOwnProperty("msg") && ret.msg !== "" ? ret.msg : __('Operation completed');
                    parent.Toastr.success(msg);
							
					var index=parent.layer.getFrameIndex(window.name);
					parent.layer.close(index);
				});
                   
            });
		},
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
